<template>
  <div class="user_page">
    <div class="user_card" v-if="user">
      <img :src="user.avatar || defaultAvatar" class="avatar" />
      <div class="info">
        <div class="account">账号：{{ user.username }}</div>
        <div class="nickname">昵称：{{ user.nickname }}</div>
        <div class="email">邮箱：{{ user.email }}</div>
        <div class="gender">性别：{{ user.sex==1? '♀' : '♂'}}</div>
      </div>
    </div>

    <div class="options">
      <router-link  class="option" to="/userForm" >账号编辑</router-link>
      <router-link  class="option" to="/userArticle">我的发布</router-link>
      <router-link  class="option" to="/userCollect">我的收藏</router-link>
      <router-link  class="option" to="/userFriend">我的好友</router-link>
      <router-link  class="option" to="/userFocus">我的关注</router-link>
      <router-link  class="option" to="/userHistory">我的历史</router-link>
    </div>
  </div>
</template>

<script setup>
import server from '@/api/server';
import { useRouter } from 'vue-router';
import { userGet } from '@/api/BlogUser';
import { ref, onMounted } from 'vue'

const router = useRouter();

const user = ref({
  username: '',
  password: '',
  nickname: '',
  email: '',
  phone: '',
  sex: '',
  avatar: ''
});


onMounted(async () => {
  if(server.token == ''){
    router.push("/notLogin")
    return
  }
  try {
    await userGet(server.token).then((res)=>{
      if(res.data.code == 0){
        user.value.username = res.data.data.username
        user.value.password = res.data.data.password
        user.value.nickname = res.data.data.nickname
        user.value.email = res.data.data.email
        user.value.phone = res.data.data.phone
        user.value.sex = res.data.data.sex
        user.value.avatar = res.data.data.avatar
      }else{

        alert('获取用户信息失败');

      }
    })
  } catch (error) {

    alert('获取用户信息失败: '+error);

  }
})
</script>
  

<style scoped>
.user_page {
  min-height: 60vh;
  background-color: #f5f7fa;
  padding: 40px 20px;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  max-width: 700px;
  margin: 0 auto;
}

.user_card {
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 24px;
  border-radius: 16px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.08);
  margin-bottom: 30px;
}

.avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  object-fit: cover;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  margin-right: 20px;
  border: 2px solid #e0e0e0;
}

.info {
  display: flex;
  flex-direction: column;
  gap: 8px;
  color: #333;
  font-size: 16px;
  line-height: 1.5;
}

.options {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  justify-content: space-between;
}

.option {
  flex: 1 1 45%;
  background-color: #fff;
  padding: 16px;
  border-radius: 12px;
  text-align: center;
  color: #444;
  font-size: 15px;
  font-weight: 500;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  transition: all 0.2s ease-in-out;
  cursor: pointer;
}

.option:hover {
  background-color: #eaf2ff;
  color: #0056d2;
  transform: translateY(-3px);
}
</style>
  